using System;
using System.IO;
using System.Text;


namespace Idp_Login
{
    public partial class PathHelper
    {
        private static StringBuilder RemoveLastChar(StringBuilder value)
        {
            return value == null || value.Length <= 0 ? value : value.Remove(value.Length - 1, 1);
        }
        private static string RemoveFirstChar(  string value, char c)
        {
            return value == null || value.Length <= 0
                ? value
                : (value[0] == c
                    ? value.Substring(1, value.Length - 1)
                    : value);
        }
        private static StringBuilder RemoveLastChar(StringBuilder value, params char[] c)
        {
            if (value == null) throw new System.ArgumentNullException(nameof(value));
            if (c == null || value.Length <= 0)
            {
                return value;
            }

            foreach (var item in c)
            {
                if (value[value.Length - 1] == item)
                {
                    //value.RemoveLastChar();
                    RemoveLastChar(value);
                    return value;
                }
            }

            return value;
        }
    }

    public partial class PathHelper
    {
        public static string PathCombine(char separator = '/', params string[] paths)
        {
            if (paths == null || paths.Length <= 0)
            {
                return "";
            }

            if (paths.Length == 1)
            {
                return paths[0];
            }

            var capacity = paths.Length - 1;
            for (int i = 0; i < paths.Length; i++)
            {
                capacity += paths[i]?.Length ?? 0;
            }
            var sb = new StringBuilder(capacity);

            if (separator == '/')
            {
                for (int i = 0; i < paths.Length; i++)
                {
                    if (i > 0 && paths[i].Length > 0)
                    {
                        if (paths[i][0] == separator)
                        {
                            //paths[i] = paths[i].RemoveFirstChar(paths[i][0]);
                            paths[i] = RemoveFirstChar(paths[i], paths[i][0]);
                        }
                    }
                    if (paths[i].Contains("\\"))
                    {
                        paths[i] = paths[i].Replace('\\', separator);
                    }
                    sb.Append(paths[i]);
                    var lastChar = sb[sb.Length - 1];
                    if (lastChar != separator)
                    {
                        //sb.RemoveLastChar('/', '\\');
                        RemoveLastChar(sb, '/', '\\');
                        sb.Append(separator);
                    }
                }
            }
            else
            {
                for (int i = 0; i < paths.Length; i++)
                {
                    sb.Append(paths[i]);
                    var lastChar = sb[sb.Length - 1];
                    if (lastChar != separator)
                    {
                        //sb.RemoveLastChar('/', '\\');
                        RemoveLastChar(sb, '/', '\\');
                        sb.Append(separator);
                    }
                }
            }

            //sb.RemoveLastChar(separator);
            RemoveLastChar(sb, separator);

            var pathResult = sb.ToString();
            return pathResult;
        }
    }
}